package com.kkwrite.regimen.consumer.controller.shopItem;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import com.kkwrite.regimen.common.entity.dto.OutDTO;
import com.kkwrite.regimen.common.entity.dto.shopItem.InsertShopItemInDTO;
import com.kkwrite.regimen.common.entity.dto.shopItem.InsertShopItemOutDTO;
import com.kkwrite.regimen.common.entity.dto.shopItem.ShopItemDTO;
import com.kkwrite.regimen.consumer.controller.constant.Constant;

/**
 * 项目店铺接口
 *
 * @author jinying
 * 
 */
@Api(value = "查询项目店铺接口")
@RestController
@RequestMapping("/shopItem")
public class ShopItemController {

	private static final Logger logger = LoggerFactory
			.getLogger(ShopItemController.class);

	@Resource(name = "restTemplate")
	private RestTemplate restTemplate;

	@ApiOperation(value = "根据项目店铺查询详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "shopKeywords", value = "店铺关键字", required = true, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "itemId", value = "项目编号", required = true, paramType = "query", dataType = "Long"),
			@ApiImplicitParam(name = "lat", value = "终端纬度", required = true, paramType = "query", dataType = "Double"),
			@ApiImplicitParam(name = "lng", value = "终端经度", required = true, paramType = "query", dataType = "Double"),
			@ApiImplicitParam(name = "city", value = "所在城市编码", required = true, paramType = "query", dataType = "String")})
	@SuppressWarnings("rawtypes")
	@GetMapping("/getShopByItem")
	public OutDTO<List<ShopItemDTO>> getShopByItem(@RequestParam Long itemId, @RequestParam String shopKeywords,@RequestParam Double lat,@RequestParam Double lng,@RequestParam String city) {
		if (itemId == null || itemId.equals("")) {
			return new OutDTO("false", "入参不合法");
		}
		OutDTO outDTO = null;
		logger.info("ShopItemController.getShopItem(), itemId = " + itemId
				+ "_" + "shopKeywords=" + shopKeywords);
		if (logger.isDebugEnabled()) {
			logger.info("ShopItemController.getShopItem(), itemId = " + itemId
					+ "_" + "opKeywords=" + shopKeywords);
		}
		try {
			String url = Constant.SERVICE_PREFIX
					+ "/shopItem/getShopByItem?itemId=" + itemId
					+ "&shopKeywords=" + shopKeywords+"&lat="+lat+"&lng="+lng+"&city="+city;
			outDTO = restTemplate.getForObject(url, OutDTO.class);
			List<ShopItemDTO> shopItemList = (ArrayList<ShopItemDTO>) outDTO.getData();
			if(shopItemList!=null){
				for(int i = 0;i<shopItemList.size();i++){
					Map shopItemMap = (Map) shopItemList.get(i);
					shopItemMap.put("num", shopItemList.size());
					if (shopItemMap != null) {
						List<LinkedHashMap> picturesList = (ArrayList<LinkedHashMap>) shopItemMap.get("pictures");
						if (picturesList != null && picturesList.size() > 0) {
							for (Map picturesMap: picturesList) {
								Integer pictureType = (Integer)picturesMap.get("pictureType");
								if (pictureType!=null && pictureType==11) {
									//屏蔽掉项目封面图
									/*shopItemMap.put("pictureUrl", picturesMap.get("pictureUrl"));*/
									shopItemMap.remove("pictures");
									break;
									
								}
							}
						}
					}
				}
			}
			return outDTO;
		} catch (RestClientException e) {
			logger.info("ShopItemController.getShopItem(), itemId = " + itemId
					+ "_" + "opKeywords=" + shopKeywords + "_" + e.getMessage());
			e.printStackTrace();
			return new OutDTO<List<ShopItemDTO>>("failed", e.getMessage());
		}
	}
	
	@PostMapping("/insertShopItem")
    @ApiOperation(value = "新增店铺项目")
	@ApiImplicitParam(name = "inDTO", value = "提交数据", required = true, paramType = "body", dataType = "InsertShopItemInDTO")
    public OutDTO insertShop(@RequestBody InsertShopItemInDTO inDto,HttpServletRequest request){
    	if(inDto.getShopId()==null||inDto.getItems()==null||inDto.getItems().length==0){
    		return OutDTO.of("faild", "店铺id和项目id为必须参数");
    	}
    	OutDTO outDto = restTemplate.postForObject(Constant.SERVICE_PREFIX+ "/shopItem/insertShopItem",inDto, OutDTO.class);
    	return outDto;
    }
}
